/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox

import com.almasb.fxgl.animation.*
import com.almasb.fxgl.app.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.entity.*
import com.almasb.fxgl.entity.components.*
import com.almasb.fxgl.input.*
import com.almasb.fxgl.physics.*
import com.windea.study.kotlin.fxgl.sample.dev.*
import javafx.geometry.*
import javafx.scene.canvas.*
import javafx.scene.input.*
import javafx.scene.paint.*
import javafx.scene.text.*
import javafx.util.*

/**
 * Shows how to init a basic game object and attach it to the world
 * using fluent API.
 *
 * @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)
 */
class InitSample : GameApplication() {
	
	private lateinit var player: Entity
	
	private var scale = 1.0
	private var scaleY = 1.0
	
	private var angle = 0.0
	
	private lateinit var t: Text
	private lateinit var g: GraphicsContext
	
	// 1. define types of entities in the game using Enum
	private enum class Type {
		PLAYER, NPC
	}
	
	override fun initSettings(settings: GameSettings) {
		settings.width = 800
		settings.height = 600
		settings.title = "InitSample"
		settings.version = "0.1"
	}
	
	override fun initInput() {
		getInput().addAction(object : UserAction("Change view") {
			override fun onActionBegin() {
				//player.setView(new EntityView(new Rectangle(40, 30, Color.BLUE)));
			}
		}, KeyCode.F)
		
		getInput().addAction(object : UserAction("Change view 1") {
			override fun onActionBegin() {
				//player.setView(texture("bird.png").toAnimatedTexture(2, Duration.seconds(0.33)).play());
			}
		}, KeyCode.G)
		
		onKey(KeyCode.Q) {
			scale += 0.1
			
			player.scaleX = scale
		}
		
		onKey(KeyCode.E) {
			scaleY += 0.1
			
			player.scaleY = scaleY
		}
		
		onKey(KeyCode.R) {
			angle -= 5.0
			
			player.rotation = angle
		}
		
		onKey(KeyCode.T) {
			angle += 5.0
			
			player.rotation = angle
		}
	}
	
	override fun initGame() {
		player = entityBuilder()
			.type(Type.PLAYER)
			.at(100.0, 150.0)
			.viewWithBBox("brick.png")
			.with(CollidableComponent(true), DeveloperWASDControl())
			.zIndex(250)
			.buildAndAttach()
		
		player.transformComponent.rotationOrigin = Point2D(64.0, 64.0)
		
		player.transformComponent.scaleOriginXProperty().setValue(64)
		player.transformComponent.scaleOriginYProperty().setValue(64)
		
		val player2 = Entity()
		player2.type = Type.NPC
		player2.setPosition(100.0, 100.0)
		//player2.setView(new EntityView(new Rectangle(40, 40, Color.RED)));
		player2.boundingBoxComponent.addHitBox(HitBox(BoundingShape.box(40.0, 40.0)))
		player2.addComponent(CollidableComponent(true))
		player2.transformComponent.z = 250
		
		getGameWorld().addEntity(player2)
		
		animationBuilder().delay(Duration.seconds(0.25))
			.duration(Duration.seconds(1.0))
			.interpolator(Interpolators.BOUNCE.EASE_OUT())
			//.repeat(2)
			.rotate(player2, player)
			.from(0.0)
			.to(360.0)
			.buildAndPlay()
		
		//translate(player, new Point2D(560, 300), Duration.seconds(2));
		
		//        player.getViewComponent().addClickListener(() -> {
		//            System.out.println("CLICKED");
		//        });
		
		//        entityBuilder()
		//                .type(Type.PLAYER)
		//                .at(120, 120)
		//                .viewWithBBox(new Rectangle(40, 40, Color.GREEN))
		//                .with(new CollidableComponent(true))
		//                .zIndex(100)
		//                .buildAndAttach();
	}
	
	override fun initPhysics() {
		getPhysicsWorld().addCollisionHandler(object : CollisionHandler(Type.PLAYER, Type.NPC) {
			override fun onCollisionBegin(a: Entity?, b: Entity?) {
				t.text = "Collision"
			}
			
			override fun onCollisionEnd(a: Entity?, b: Entity?) {
				t.text = "No collision"
			}
		})
	}
	
	override fun initUI() {
		t = Text()
		t.translateY = 100.0
		
		getGameScene().addUINode(t)
		
		val canvas = Canvas(getAppWidth().toDouble(), getAppHeight().toDouble())
		g = canvas.graphicsContext2D
		g.fill = Color.YELLOWGREEN
		
		//getGameScene().addUINode(canvas);
	}
	
	override fun onUpdate(tpf: Double) {
		g.clearRect(0.0, 0.0, getAppWidth().toDouble(), getAppHeight().toDouble())
		
		//var points = Debug.INSTANCE.getPoints();
		//
		//while(!points.isEmpty()) {
		//	var p = points.poll();
		//
		//	g.fillOval(p.getX() - 2.5, p.getY() - 2.5, 5, 5);
		//}
	}
}

fun main(args: Array<String>) {
	GameApplication.launch(InitSample::class.java, args)
}

